package com.team.app.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.team.app.enums.ObjectType;
import com.team.app.model.Auth;
import com.team.app.model.Role;

@Repository
public class RoleDaoImpl extends AbstractDao<Role> implements RoleDao {
	public RoleDaoImpl(){
		super(Role.class);	
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Role> findAll(Auth loginAuth,Role obj) {
		if(loginAuth.getObjectType().equals(ObjectType.TYPE_SERVICE.getKey())){
			String hql = "from Role o where o.roleId <> " + loginAuth.getRoleId();
			if(obj!=null){
				if(obj.getRoleName() != null){
					hql += " and o.roleName like '%"+obj.getRoleName().trim() +"%'";
				}
				if(obj.getRoleDescription() != null){
					hql += " and o.roleDescription like '%"+obj.getRoleDescription().trim() +"%'";
				}
			}			
			Session s=this.getCurrentSession();
			Query q = s.createQuery(hql);			
			List<Role> result = q.list();		
			
			return result;
		}else{
			String hql = "from Role o where  o.roleId not in (1,98) and o.roleId <>  " + loginAuth.getRoleId();
			if(obj!=null){
				if(obj.getRoleName() != null){
					hql += " and o.roleName like '%"+obj.getRoleName().trim() +"%'";
				}
				if(obj.getRoleDescription() != null){
					hql += " and o.roleDescription like '%"+obj.getRoleDescription().trim() +"%'";
				}
			}			
			Session s=this.getCurrentSession();
			Query q = s.createQuery(hql);			
			List<Role> result = q.list();		
			return result;
		}			
	}
}
